<?php
/**************************************************************************
	ESAM (EVE to SMF API Module)
	Copyright (C) 2009  Sasha J

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

if (!defined('SMF')) die('Hacking attempt...');

function EveSettings()
{
	global $context, $txt, $scripturl;

	isAllowedTo('manage_bans'); // Any admin role can do

	// Boldify "Ban Members" on the admin bar.
	adminIndex('evesettings');

	loadTemplate('EveSettings');

	$context['admin_tabs'] = array(
		'title' => &$txt['eve_settings'],
		'help' => 'ban_members',
		'description' => $txt['eve_settings'],
		'tabs' => array(
			'index' => array(
				'title' => $txt['eve_general_settings'],
				'description' => $txt['eve_general_settings'].'<br /><strong>'.$txt['eve_api_dt_warning'].'</strong>',
				'href' => $scripturl . '?action=evesettings;sa=index',
				'is_selected' => $_REQUEST['sa'] == 'index',
			),
			'alliances' => array(
				'title' => $txt['eve_load_alliance_data'],
				'description' => $txt['eve_load_alliance_data_desc'].'<br /><strong>'.$txt['eve_api_dt_warning'].'</strong>',
				'href' => $scripturl . '?action=evesettings;sa=alliance',
				'is_selected' => $_REQUEST['sa'] == 'alliance',
			),
			'corp' => array(
				'title' => $txt['eve_add_corporations'],
				'description' => $txt['eve_add_corporations_desc'].'<br /><strong>'.$txt['eve_api_dt_warning'].'</strong>',
				'href' => $scripturl . '?action=evesettings;sa=corp',
				'is_selected' => $_REQUEST['sa'] == 'corp',
			),
			'rules' => array(
				'title' => $txt['eve_manage_rules'],
				'description' => $txt['eve_manage_rules_desc'].'<br /><strong>'.$txt['eve_api_dt_warning'].'</strong>',
				'href' => $scripturl . '?action=evesettings;sa=rules',
				'is_selected' => $_REQUEST['sa'] == 'rules',
			),
			'validate' => array(
				'title' => $txt['eve_validate'],
				'description' => $txt['eve_validate'],
				'href' => $scripturl . '?action=evesettings;sa=validate',
				'is_selected' => $_REQUEST['sa'] == 'validate',
			),
			'about' => array(
				'title' => $txt['eve_about'],
				'description' => $txt['eve_about'],
				'href' => $scripturl . '?action=evesettings;sa=about',
				'is_selected' => $_REQUEST['sa'] == 'about',
			),
		),
	);

    $subActions = array(
        'index' => 'EveSettingsIndex',
		'alliance' => 'EveSettingsAlliance',
        'corp' => 'EveSettingsCorp',
        'remove_corp' => 'EveSettingsRemoveCorp',
        'add_corps' => 'EveSettingsAddCorps',
        'rules' => 'EveSettingsRules',
        'remove_rule' => 'EveSettingsRemoveRule',
        'about' => 'EveSettingsAbout',
        'validate' => 'EveValidate',
	);
    $subActions[$_REQUEST['sa']]();
}

function EveValidate() {
	global $context;
	$context['sub_template'] = 'evevalidate';
	$context['extra_text'] = revalidateEveUsers();
}

function EveSettingsIndex() {
    global $context;
    if(isset($_REQUEST["submit"])) {
        if(isset($_REQUEST['need_to_enter'])) {
            saveEveSetting('GLOBAL_REQUIRED_API', 0);
        } else {
            saveEveSetting('GLOBAL_REQUIRED_API', 1);
        }
        $context['extra_text'] = $txt['eve_settings_saved'];
    }
}

function EveSettingsAbout() {
    global $context;
    $context['sub_template'] = 'eveabout';
}

function EveSettingsRules() {
    global $context, $txt;
    // POST
    if(isset($_REQUEST['alliances'])) {
        $corp = $alliance = null;
        if(isset($_REQUEST['selection']) && $_REQUEST['selection'] == 'alliance') {
            $alliance = $_REQUEST['alliances'];
        } else {
            $corp = $_REQUEST['corp'];
        }
        addEveRule($_REQUEST['group'], $alliance, $corp, $_REQUEST['role']);
        $context['extra_text'] = $txt['eve_rule_added'];
    }
    $context['everules'] = getEveRules();
    $context['sub_template'] = 'rules';
    $context['alliances'] = getAlliances();
    $context['corp'] = getAllCorporations();
    $context['groups'] = getUserGroups();
}

function EveSettingsRemoveRule() {
    global $context, $txt;
    if(isset($_REQUEST['id'])) {
        removeEveRule($_REQUEST['id']);
        $context['extra_text'] = $txt['eve_rule_removed'];
    }
    $context['everules'] = getEveRules();
    $context['sub_template'] = 'rules';
    $context['alliances'] = getAlliances();
    $context['corp'] = getAllCorporations();
    $context['groups'] = getUserGroups();
}

function EveSettingsAddCorps() 
{
    global $context, $txt;
    if(isset($_REQUEST['alliances'])) 
    {
        fillCorpsFromAlliance($_REQUEST['alliances']);
        $context['extra_text'] = $txt['eve_corporations_added'];
    } else if (isset($_REQUEST['characterID']))
    {
    	addCorporation($_REQUEST['apiID'], $_REQUEST['apiKey'], $_REQUEST['characterID']);
        $context['extra_text'] = $txt['eve_corporations_added'];
    }
    $context['corp'] = getAllCorporations();
    $context['sub_template'] = 'corp';
    $context['alliances'] = getAlliances();
}

function EveSettingsRemoveCorp() {
    global $context, $txt;
    if(isset($_REQUEST['id'])) {
        removeCorporation($_REQUEST['id']);
        $context['extra_text'] = $txt['eve_corporation_removed'];
    }
    $context['corp'] = getAllCorporations();
    $context['sub_template'] = 'corp';
    $context['alliances'] = getAlliances();
}
function EveSettingsCorp() {
    global $context;
    $context['corp'] = getAllCorporations();
    $context['sub_template'] = 'corp';
    $context['alliances'] = getAlliances();
}

function EveSettingsAlliance() {
    global $context;
    fillAllianceData();
    $context['alliances'] = getAlliances();
    $context['sub_template'] = 'alliances';
}
?>
